<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
    <style>
        .selectInput {
            position: relative;
            width: 100%;
            min-width: 100px;
        }

        .selectInput .flavorSelect {
            position: absolute;
            width: 100%;
            padding: 0 10px;
            border-radius: 3px;
            border: solid 1px #FF903D;
            line-height: 30px;
            text-align: center;
            background: #fff;
            top: 50px;
            z-index: 99;
        }

        .selectInput .flavorSelect .items {
            cursor: pointer;
            display: inline-block;
            width: 100%;
            line-height: 35px;
            border-bottom: solid 1px #f4f4f4;
            color: #666;
        }

        .selectInput .flavorSelect .none {
            font-size: 14px;
        }

        #food-add-app .uploadImg .el-form-item__label::before {
            content: '*';
            color: #F56C6C;
            margin-right: 4px;
        }

    </style>
</head>
<body>
<div class="addBrand-container" id="member-add-app">
    <div class="container">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" :inline="true" label-width="180px"
                 class="demo-ruleForm">
            <div>
                <el-form-item label="账号名" prop="username">
                    <el-input v-model="ruleForm.sysUser.username" placeholder="请填写会员账号名" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <div>
                    <el-form-item label="头像:" prop="head" class="uploadImg">
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:8080/common/upload"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess1"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                </div>
            </div>
            <div>
                <div>
                    <el-form-item label="形象照:" prop="image" class="uploadImg">
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:8080/common/upload"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess2"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="imageUrl2" :src="imageUrl2" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                </div>
            </div>

            <div>
                <el-form-item label="会员名字" prop="name">
                    <el-input v-model="ruleForm.sysUser.name" placeholder="请填写会员姓名" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="ruleForm.sysUser.nickname" placeholder="请填写会员昵称" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="年龄:" prop="age">
                    <el-input v-model="ruleForm.sysUser.age" placeholder="请填写会员年龄" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="性别:" prop="gender">
                    <el-radio-group v-model="ruleForm.sysUser.gender">
                        <el-radio label="男">男</el-radio>
                        <el-radio label="女">女</el-radio>
                    </el-radio-group>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="生日:" prop="birthday">
                    <el-date-picker v-model="ruleForm.sysUser.birthday" type="date" placeholder="选择日期"
                                    @change="timeupdate()">
                    </el-date-picker>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="身份证号:" prop="idNumber">
                    <el-input v-model="ruleForm.sysUser.idNumber" placeholder="请填写会员身份证号" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="电话:" prop="phone">
                    <el-input v-model="ruleForm.sysUser.phone" placeholder="请填写会员电话" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="邮箱:" prop="email">
                    <el-input v-model="ruleForm.sysUser.email" placeholder="请填写会员邮箱" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="店铺名称:" prop="shopkeeperName">
                    <el-select v-model="ruleForm.shopkeeperName" placeholder="请选择">
                        <el-option v-for="item in shopkeepers" :key="item" :label="item" :value="item">{{item}}
                        </el-option>
                    </el-select>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="会员标签">
                    <el-select v-model="ruleForm.sysUser.tag" multiple placeholder="请选择标签">
                        <el-option
                                v-for="tag in tagOptions"
                                :key="tag"
                                :label="tag"
                                :value="tag">
                        </el-option>
                    </el-select>
                </el-form-item>
            </div>

            <div>
                <el-form-item label="等级:" prop="level">
                    <el-select v-model="ruleForm.level" placeholder="请选择">
                        <el-option
                                v-for="item in levelOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>
            </div>
            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()">取消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                    <el-button v-if="actionType == 'add'" type="primary" class="continue"
                               @click="submitForm('ruleForm','goAnd')">
                        保存并继续添加会员
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script src="../../api/member.js"></script>
<script src="../../api/student.js"></script>
<script src="../../api/shop.js"></script>
<script src="../../js/common.js"></script>
<script src="../../api/shopkeeper.js"></script>
<script>
    new Vue({
        el: '#member-add-app',
        data() {
            return {
                shopkeepers: [],
                id: '',
                value1: '',
                imageUrl1: '',
                imageUrl2: '',
                actionType: '',
                levelOptions: [
                    {value: '1', label: 'vip'},
                    {value: '2', label: 'svip'},
                    {value: '3', label: 'ssvip'}
                ],
                ruleForm: {
                    username: '',
                    name: '',
                    nickname: '',
                    age: '',
                    gender: '男',
                    birthday: '',
                    idNumber: '',
                    phone: '',
                    email: '',
                    shopkeeperId: '',
                    tag: '',
                    level: '',
                    workTime: '',
                    good: '',
                    experience: '',
                    head: '',
                    images: '',
                    sysUser: {}
                },
                tagOptions: [],
                rules: {
                    // name: [
                    //     {required: true, message: '请填写名称', trigger: 'blur'}
                    // ],
                    // idNumber: [
                    //     {required: true, message: '请填写身份证号', trigger: 'blur'},
                    //     {validator: this.validateIdNumber, trigger: 'blur'}
                    // ],
                    // phone: [
                    //     {required: true, message: '请填写电话', trigger: 'blur'},
                    //     {validator: this.validatePhone, trigger: 'blur'}
                    // ],
                    // email: [
                    //     {required: true, message: '请填写邮箱', trigger: 'blur'},
                    //     {type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change']}
                    // ]
                }
            }
        },
        created() {
            //和修改相关
            this.userId = requestUrlParam('userId');
            this.actionType = this.userId ? 'edit' : 'add';
            console.log(this.actionType);
            this.init2()
            //根据标签类型获取标签名字
            getTagListByType("会员").then(res => {
                this.tagOptions = res.data;
            })
        },
        mounted() {
        },
        methods: {
            async init2() {
                var _this = this
                getAllShopkeeperName().then(res => {
                    _this.shopkeepers = res.data
                    console.log(_this.shopkeepers)
                })
            },
            handleAvatarSuccess1(res, file) {
                console.log(res.data);
                this.ruleForm.sysUser.head = res.data
                console.log(this.ruleForm.head);
                console.log(file);
                this.imageUrl1 = URL.createObjectURL(file.raw);
            },
            handleAvatarSuccess2(res, file) {
                console.log(res.data);
                this.ruleForm.sysUser.images = res.data
                console.log(this.ruleForm.images);
                console.log(file);
                this.imageUrl2 = URL.createObjectURL(file.raw);
            },
            async init() {
                queryTeacherById(this.id).then(res => {
                    console.log(res);
                    if (String(res.code) === '1') {
                        this.ruleForm = {...res.data};
                        this.ruleForm.status = res.data.status == '1'
                        // this.imageUrl = `/common/download?name=${res.data.image}`
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            timeupdate() {
                var t = dateFormat("yyyy-MM-dd", this.ruleForm.sysUser.birthday)
                console.log(t)
                this.ruleForm.birthday = t
            },
            submitForm(formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const params = {
                            ...this.ruleForm,
                            ...this.ruleForm.sysUser.tag = this.ruleForm.sysUser.tag.toString()
                            // sex: this.ruleForm.sex === '女' ? '0' : '1',
                            //                      head:this.imageUrl.split("=")[1],
                            // image:this.imageUrl2.split("=")[1]
                        };
                        console.log(this.imageUrl)
                        console.log(this.imageUrl2)
                        params.status = this.ruleForm ? 1 : 0;
                        if (this.actionType == 'add') {
                            delete params.id;
                            addMember(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('会员添加成功！');
                                    if (!st) {
                                        window.parent.goBack();
                                    } else {
                                        this.imageUrl = '';
                                        this.ruleForm = {
                                            'id': '',
                                            'name': '',
                                            'email': '',
                                            'shop': '',
                                            'phone': '',
                                            'level': '',
                                        }
                                    }
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else {
                            delete params.updateTime;
                            editTeacher(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('会员修改成功！');
                                    window.parent.goBack();
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        return false
                    }
                })
            },
            handleCheckedCitiesChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.tags.length;
                this.isIndeterminate = checkedCount > 0 && checkedCount < this.tags.length;
            },
            onChange(file) {
                if (file) {
                    const suffix = file.name.split('.')[1];
                    const size = file.size / 1024 / 1024 < 2;
                    if (['png', 'jpeg', 'jpg'].indexOf(suffix) < 0) {
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！');
                        this.$refs.upload.clearFiles();
                        return false
                    }
                    if (!size) {
                        this.$message.error('上传文件大小不能超过 2MB!');
                        return false
                    }
                    return file
                }
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            // 验证身份证号 (18位)
            validateIdNumber(rule, value, callback) {
                if (!value) {
                    callback(new Error('请填写身份证号'));
                } else if (!/^\d{17}[\dXx]$/.test(value)) {
                    callback(new Error('请输入18位有效身份证号'));
                } else {
                    callback();
                }
            },

            // 验证手机号 (11位)
            validatePhone(rule, value, callback) {
                if (!value) {
                    callback(new Error('请填写电话'));
                } else if (!/^1[3-9]\d{9}$/.test(value)) {
                    callback(new Error('请输入11位有效手机号码'));
                } else {
                    callback();
                }
            },
            // 验证电子邮件
            validateEmail(rule, value, callback) {
                if (!value) {
                    callback(new Error('请填写邮箱'));
                } else if (!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value)) {
                    callback(new Error('请输入有效的邮箱地址'));
                } else {
                    callback();
                }
            },
        }
    })
</script>
</body>
</html>